package com.tevintao.marketingsystem.task;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;

import com.tevintao.marketingsystem.commen.UrlConfig;
import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.provider.ChatFriendBean;
import com.tevintao.marketingsystem.provider.ChatNewChatDB;
import com.tevintao.marketingsystem.services.AccountManager;
import com.tevintao.marketingsystem.tool.ImageTool;
import com.tevintao.marketingsystem.tool.UploadFileTool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


/**
 * 上报账号个人头像 和  上报  好友头像
 */
public class ReportIcon {

    public static final String iconPath = AccountManager.SD_MS_ROOT + "/icon";
    public static final String accountIconName = iconPath + "/accountName.jpg";

    private static ReportIcon ourInstance = new ReportIcon();

    public static ReportIcon getInstance() {
        return ourInstance;
    }

    private ReportIcon() {
    }


    public static void checkIconPath() {
        File iconDir = new File(iconPath);
        if (!iconDir.exists()) {
            iconDir.mkdir();
        }
    }

    public static boolean saveAccountIcon(Bitmap bitmap) {
        checkIconPath();

        FileOutputStream fos;
        try {
            File imageFile = new File(accountIconName);
            if (imageFile.exists()) {
                LogManager.getInstance().logError("文件已经存在 " + imageFile.getName());
                imageFile.delete();
            }
            imageFile.createNewFile();
            fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("savePic FileNotFoundException ");
        } catch (IOException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("savePic IOException ");
        }
        return false;
    }

    private static final int MAX_RETRY_TIMES = 3;

    public void reportAccountIcon(String account) {
        for (int i = 0; i < MAX_RETRY_TIMES; i++) {
            String result = UploadFileTool.uploadFile(String.format(UrlConfig.URL_REPORT_ACCOUNT_ICON, account), accountIconName, account + ".jpg");
            if (!TextUtils.isEmpty(result)) {
                LogManager.getInstance().logInfo("上传个人头像成功" + result);
                break;
            }
            if (i >= MAX_RETRY_TIMES - 1) {
                LogManager.getInstance().logInfo("上传个人头像多次失败");
            }
        }
    }

    public static boolean checkIconExits(String wxId) {
        File imageFile = new File(iconPath + "/ " + wxId + ".jpg");
        return imageFile.exists();
    }

    public static boolean saveFriendIcon(Bitmap bitmap, String wxId) {
        checkIconPath();

        if (null == bitmap) {
            LogManager.getInstance().logError("获取图片失败");
            return false;
        }

        FileOutputStream fos;
        try {
            File imageFile = new File(iconPath + "/" + wxId + ".jpg");
            if (imageFile.exists()) {
                LogManager.getInstance().logError("文件已经存在 " + imageFile.getName());
                return true;
            }
            imageFile.createNewFile();
            fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("savePic FileNotFoundException ");
        } catch (IOException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("savePic IOException ");
        }

        return false;
    }

    public void reportFriendIcon(final String account) {
        ChatFriendBean bean = ChatFriendBean.getOneNoUpdateFriend();
        if (null == bean) {
            LogManager.getInstance().logError("reportFriendIcon 好友图像上报完毕");
            return;
        }
        String url = String.format(UrlConfig.URL_REPORT_CHAT_FRIEND_ICON, account, bean.getUsername());
        LogManager.getInstance().logInfo("开始上报 名字 " + bean.getNickname() + " url = " + url + "path = " + bean.getCoverPath());
        for (int i = 0; i < MAX_RETRY_TIMES; i++) {
            String result = UploadFileTool.uploadFile(url, bean.getCoverPath());
            if (!TextUtils.isEmpty(result) && !result.contains("not")) {
                ChatNewChatDB.getInstance().updateFriendIconSuccess(bean.getUsername());
                LogManager.getInstance().logInfo("上传好友头像成功" + result);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        reportFriendIcon(account);
                    }
                }).start();
                break;
            }
            if (i >= MAX_RETRY_TIMES - 1) {
                LogManager.getInstance().logInfo(bean.getNickname() + " 上传好友头像失败" + result);
            }
        }


    }

}
